﻿using System;
using System.Collections.Generic;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Bo;
using Monkey.Warrenty.Constants;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.WarrantyTickets
{
    public partial class Print : System.Web.UI.Page
    {
        #region Properties

        private WarrantyTicketBiz _warrantyTicketBiz;
        private WarrantyTicketBiz WarrantyTicketManager
        {
            get { return _warrantyTicketBiz ?? (_warrantyTicketBiz = new WarrantyTicketBiz()); }
        }

        private long _ticketId;
        private long TicketId
        {
            get
            {
                if (_ticketId <= 0)
                    long.TryParse(Request.Params["id"], out _ticketId);
                return _ticketId;
            }
        }

        protected List<TaskServiceBo> TaskProducts;
        protected List<TaskServiceBo> TaskLendProducts;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.AdminBaoHanh))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!Page.IsPostBack)
            {
                if (TicketId > 0)
                {
                    var warrantyTicketForPrint = WarrantyTicketManager.GetWarrantyTicketForPrint(TicketId);
                    if (warrantyTicketForPrint != null && warrantyTicketForPrint.Count > 0)
                    {

                        var warrantyTicketPrint = warrantyTicketForPrint[0];

                        ltrCode.Text = warrantyTicketPrint.WarrantyTicketCode;
                        ltrTicketDate.Text = string.Format("{0:dd/MM/yyyy HH:mm}", warrantyTicketPrint.WarrantyTicketDate);
                        ltrCustomerName.Text = warrantyTicketPrint.CustomerName;
                        ltrCustomerPhone.Text = warrantyTicketPrint.CustomerPhone;
                        ltrCustomerAddress.Text = warrantyTicketPrint.CustomerAddress;
                        ltrDescription.Text = warrantyTicketPrint.WarrantyTicketContent;
                        //ltrLendEmployee.Text = warrantyTicketPrint.Employee2;
                    }

                    TaskProducts = WarrantyTicketManager.GetWarrantyTicketProductsForPrint(TicketId) ??
                                   new List<TaskServiceBo>();

                    TaskLendProducts = WarrantyTicketManager.GetWarrantyTicketLendProductsForPrint(TicketId) ??
                                       new List<TaskServiceBo>();
                }
                
                //ltrDate.Text = string.Format("{0:dd/MM/yyyy HH:mm:ss}", DateTime.Now);
            }
        }
    }
}